package com.zz.parkingsystem.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zz.parkingsystem.dto.AdminDTO;
import com.zz.parkingsystem.dto.AdminLoginDTO;
import com.zz.parkingsystem.entity.Admin;
import com.zz.parkingsystem.mapper.AdminMapper;
import com.zz.parkingsystem.service.AdminService;
import com.zz.parkingsystem.util.MD5Util;
import com.zz.parkingsystem.util.NumberUtil;
import com.zz.parkingsystem.vo.PageResult;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.zz.parkingsystem.util.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @author zz
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    /**
     * 在查询中加入分页功能
     *
     * @param adminDTO
     * @return
     */
    @Override
    public PageResult queryAllAdmin(AdminDTO adminDTO) {
        Integer pageIndex = adminDTO.getPageIndex();
        Integer pageSize = adminDTO.getPageSize();
        //查询数据
        PageHelper.startPage(adminDTO.getPageIndex(), adminDTO.getPageSize());
        List<Admin> list = adminMapper.queryAllAdmin(adminDTO);
        //处理性别和状态
        for (Admin admin:list) {
            if ("0".equals(admin.getAdminGender())){
                admin.setAdminGender("男");
            }else {
                admin.setAdminGender("女");
            }
            if("0".equals(admin.getAdminStatus())){
                admin.setAdminStatus("正常");
            }else{
                admin.setAdminStatus("冻结");
            }
        }

        PageInfo pageInfo = new PageInfo(list);
        PageHelper.clearPage();
        //返回一个自定义分页结果
        PageResult pageResult = new PageResult();
        pageResult.setPageIndex(pageIndex);
        pageResult.setPageSize(pageSize);
        pageResult.setPages(pageInfo.getPages());
        pageResult.setTotal((int)pageInfo.getTotal());
        pageResult.setList(list);
        return pageResult;
    }

    @Override
    public Admin selAdminByTel(String tel) {
        Admin admin = adminMapper.selAdminByTel(tel);
        return admin;
    }

    @Override
    public String adminRegist(Admin admin) {
        //给密码加盐加密
        String salt = NumberUtil.genRandomNum(5) + "";
        admin.setAdminSalt(salt);
        String password = MD5Util.MD5Encode(admin.getAdminPassword() + salt, "UTF-8");
        admin.setAdminPassword(password);
        if (adminMapper.insertSelective(admin) > 0) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    @Override
    public Admin adminLogin(AdminLoginDTO admin) {
        //给密码加盐加密
        String password = MD5Util.MD5Encode(admin.getAdminPassword() + admin.getAdminSalt(), "UTF-8");
        admin.setAdminPassword(password);
        return adminMapper.adminLogin(admin);
    }

    @Override
    public String adminUpdatePassword(AdminLoginDTO admin) {
        Admin admin1 = new Admin();
        admin1.setAdminId(admin.getAdminId());
        //给新密码加盐加密
        String newPassword = MD5Util.MD5Encode(admin.getNewPassword() + admin.getAdminSalt(), "UTF-8");
        admin1.setAdminPassword(newPassword);
        int i = adminMapper.updateByPrimaryKeySelective(admin1);
        if (i > 0) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    @Override
    public int updateByPrimaryKeySelective(Admin record) {
        return adminMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(Admin record) {
        return adminMapper.insertSelective(record);
    }

    @Override
    public Admin selectByPrimaryKey(Long id) {
        return adminMapper.selectByPrimaryKey(id);
    }
}

